package lambda;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 江岸
 * @version V1.0
 * @ClassName: Test2
 * @description: Stream中常用方法
 * @date 2020/9/2316:38
 * @since V1.0
 */
public class Test2 {
    public static void main(String[] args) {
        fun3();
    }

    //匿名类简写
    /*
    (params) -> expression
    (params) -> statement
    (params) -> { statements }
     */
    public static void fun1(){
        new Thread(() -> System.out.println("GO!GO!GO!")).start();
    }

    //forEach
    public static void fun2(){
        List<String> strings = Arrays.asList("a", "b", "c", "d", "e");
        //strings.forEach(n -> System.out.println(n));
        strings.forEach(System.out::println);
    }
    //方法引用
    public static void fun3(){
        //构造引用
        //supplier是个接口，有一个get()方法;每次调用get()方法时都会调用构造方法创建一个新对象。
//        Supplier<Product> p = (() -> new Product());
//        Supplier<Product> sup = Product::new;
//        Product product = sup.get();
//        Runnable aNew = Product::new;
//        aNew.run();
        //对象::实例方法 Lambda表达式的(形参列表)与实例方法的(实参列表)类型，个数是对应
        List<String> strings = Arrays.asList("a", "b", "c", "d", "e");
        Set set =  new HashSet();
        set.addAll(strings);
        set.forEach(t -> System.out.println(t));
        set.forEach(System.out::print);
        //类名::静态方法
        Stream<Double> stream = Stream.generate(Math::random).limit(5);
        List<Double> collect = stream.collect(Collectors.toList());
        System.out.println(collect);
        //类名::实例方法
        //TreeSet<String> set2 = new TreeSet<>((s1, s2) -> s1.compareTo(s2));
        TreeSet<String> set2 = new TreeSet<>(String::compareTo);
    }

}
